package com.wuliu.dao.impl;

import com.wuliu.dao.SecondImageDAO;
import com.wuliu.model.SecondImage;
import com.wuliu.util.Jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SecondImageDAOImpl implements SecondImageDAO {
    @Override
    public void add(SecondImage secondImage) {
        try {
            Jdbc jdbc = new Jdbc();
            Connection connection=jdbc.connection();
            String sql="insert into second_image values(null,?)";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,secondImage.getImg());
            System.out.println(secondImage);
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void deleteById(int id) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="delete from second_image where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void update(SecondImage secondImage) {
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="update second_image set img=? where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,secondImage.getImg());
            statement.setInt(2,secondImage.getId());
            statement.execute();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

    @Override
    public SecondImage findById(int id) {
        SecondImage secondImage =null;

        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from second_image where id=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            ResultSet set =statement.executeQuery();
            while (set.next()){
                String img=set.getString("img");
                secondImage=new SecondImage(id,img);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return secondImage;

    }

    @Override
    public List<SecondImage> findAll() {
        List<SecondImage> secondImages=new ArrayList<>();
        try {
            Jdbc jdbc=new Jdbc();
            Connection connection=jdbc.connection();
            String sql="select * from second_image";
            PreparedStatement statement=connection.prepareStatement(sql);
            ResultSet set=statement.executeQuery();
            while (set.next()){
                int id=set.getInt("id");
                String img=set.getString("img");
                SecondImage secondImage =new SecondImage(id,img);
                secondImages.add(secondImage);

            }
            set.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return secondImages;
    }
}
